<template>
	<view class="rentgrab">
		<view class="navlist">
			<view :class="isActive == index ?'nav_item active':'nav_item'" v-for="(item,index) in navlist" :key="index"
				@click="changeNav(index)">
				{{item.text}}
			</view>
		</view>
		<view class="all-finish" v-if="isActive == 1">
			<text v-if="!is_edit" @click="chooseAll">批量确认</text>
			<text v-else @click="finish">完成</text>
		</view>
		<view class="shoplist">
			<view class="nonelist" v-if="list.length == 0">
				<view class="shop_text">
					-- 暂无订单 --
				</view>
			</view>
			<view class="" v-else>
				<view class="shop_item" v-for="(item,index) in list" :key="index"
					@click="toPage('./rewarddetail?id='+item.id)">
					<view class="title" @click.stop="chooseItem(item)">
						<view class="left">
							<view :class="item.checked ?'check on' :'check'" v-if="is_edit">
							</view>
							<text>订单号 :</text>
							<text>{{item.order_no}}</text>
						</view>
						<text class="order_type">{{item.state_text}}</text>
					</view>
					<view class="infobox">
						<view class="right">
							<view class="infoli">
								<text class="infotitle">{{item.orders.title}}</text>
							</view>
							<view class="infoli">
								<text class="infotitle price red">{{item.orders.desc}}</text>
							</view>
						</view>
					</view>
					<view class="infobot money" v-for="(rolename,rindex) in item.roles_name" :key="rindex">
						<text>角色名：{{rolename.name}}</text>
					</view>
					<view class="infobot money">
						<text style="color: #777;">接单时间：{{item.create_time}}</text>
						<text>金额：￥{{item.amount}}</text>
					</view>
					<view class="infobot">
						<button v-if="item.train_btn" :loading="loading" :disabled="loading" class="type"
							@click.stop="offerTraining(item.id)">代练中</button>
						<button v-if="item.confirm_btn" :loading="loading" :disabled="loading" class="type"
							@click.stop="offerConfirm(item.id)">确认完成</button>
						<text class="type">查看详情</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		offerList,
		offerConfirm,
		offerTraining,
		offerConfirmMultiple,
	} from "@/api/offer.js"
	export default {
		data() {
			return {
				isActive: 0,
				is_edit: false,
				navlist: [{
						text: '已接单',
						type: '5',
						status: 'received'
					},
					{
						text: '代练中',
						type: '21',
						status: 'training'
					},
					{
						text: '已确认',
						type: '0',
						status: 'confirm'
					},
					{
						text: '已完成',
						type: '1',
						status: 'complete'
					}
				],
				config: [],
				status: '5',
				list: [],
				page: 1,
				last_page: 1,
				loading: false
			}
		},
		components: {},
		onLoad(e) {
			if (e.id) {
				this.isActive = e.id
			}
		},
		onShow() {
			this.page = 1
			this.last_page = 1
			this.list = []
			this.getData()
		},
		onReachBottom() {
			if (this.page < this.last_page) {
				this.page++
				this.getData()
			}
		},
		methods: {
			finish() {
				var that = this
				that.is_edit = false
				let ids = []
				that.list.forEach(item => {
					if (item.checked) ids.push(item.id)
				})
				if (ids.length == 0) return
				that.loading = true
				offerConfirmMultiple({
					ids
				}).then(res => {
					that.loading = false
					if (res.code == 0) {
						that.isActive = 2
						that.clear()
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			chooseItem(item) {
				item.checked = !item.checked
			},
			chooseAll() {
				this.is_edit = true
			},
			offerTraining(id) {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '是否修改该订单状态?',
					success(res) {
						if (res.confirm) {
							that.loading = true
							offerTraining({
								id
							}).then(data => {
								uni.$showMsg(data.msg)
								that.loading = false
								if (data.code == 0) {
									that.isActive = 1
									that.clear()
								}
							})
						}
					}
				})
			},
			offerConfirm(id) {
				uni.navigateTo({
					url: `/pages/order/upload?id=${id}&type=offer`
				})
				return
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '是否确认完成订单?',
					success(res) {
						if (res.confirm) {
							that.loading = true
							offerConfirm({
								id
							}).then(data => {
								uni.$showMsg(data.msg)
								if (data.code == 0) {
									that.isActive = 2
									that.clear()
								} else {
									that.loading = false
								}
							})
						}
					}
				})
			},
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
			changeNav(index) {
				this.isActive = index
				this.clear()
			},
			clear() {
				this.page = 1
				this.last_page = 1
				this.list = []
				this.getData()
			},
			getData() {
				uni.showLoading({
					mask: true
				})
				offerList({
					status: this.navlist[this.isActive].status,
					page: this.page
				}).then(res => {
					uni.hideLoading()
					if (res.code == 0) {
						this.list = this.list.concat(res.data.data)
						this.last_page = res.data.last_page
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.rentgrab {
		background-color: #f2f2f2;
		min-height: 100vh;
		/* #ifdef H5 */
		min-height: calc(100vh - 44px);
		/* #endif */
		height: 100%;

		.back_red {
			background-color: #050B18;
			color: #fff;
		}

		.back_green {
			background-color: green;
			color: #fff;
		}

		.all-finish {
			position: sticky;
			padding: 10px;
			display: flex;
			justify-content: flex-end;
			top: 40px;
			/* #ifdef H5 */
			top: 84px;
			/* #endif */
			width: 100%;
			max-width: 500px;
			z-index: 999;
			background: #f2f2f2;
			box-sizing: border-box;

			text {
				background: #050B18;
				color: #fff;
				padding: 4px 8px;
				font-size: 12px;
				border-radius: 5px;
			}
		}

		.navlist {
			display: flex;
			position: sticky;
			top: 0;
			/* #ifdef H5 */
			top: 44px;
			/* #endif */
			width: 100%;
			max-width: 500px;
			z-index: 999;
			background: #050B18;

			.nav_item {
				flex: 1;
				text-align: center;
				color: #ffffff80;
				height: 40px;
				justify-content: center;
				// line-height: 50px;
				font-weight: bold;
				display: flex;
				flex-direction: column;
				font-size: 14px;
			}

			.active {
				color: #fff;
				position: relative;
			}

			.active::after {
				position: absolute;
				content: '';
				width: 25px;
				height: 3px;
				background: #fff;
				left: 50%;
				bottom: 4px;
				transform: translate(-50%);
			}
		}

		.shoplist {

			.shop_text {
				text-align: center;
				color: #999;
				font-size: 14px;
				padding-top: 40px;
				background: transparent;
			}

			.shop_item {
				background: #fff;
				margin-top: 15px;
				width: 96%;
				margin: 10px auto;
				border-radius: 5px;
				box-sizing: border-box;
				padding: 5px 0;
				border-bottom: 5px solid #f8f8f8;

				.title {
					color: #000;
					font-size: 14px;
					padding: 10px;
					border-bottom: 1px solid #f3f3f3;
					display: flex;
					align-items: center;
					justify-content: space-between;

					text {
						margin-right: 5px;
					}

					.order_type {
						color: #050B18;
					}

					.left {
						display: flex;
						align-items: center;
					}

					.check {
						border-radius: 50%;
						width: 12px;
						height: 12px;
						border: 1px solid #ccc;
						margin-right: 5px;
						padding: 2px;

						&.on {
							background: #050B18;
							position: relative;

							&::after {
								content: "✔";
								position: absolute;
								top: 50%;
								left: 50%;
								transform: translate(-50%, -50%);
								font-size: 12px;
								color: #fff;
							}
						}
					}
				}

				.infobox {
					display: flex;
					align-items: center;
					padding: 10px;
					box-sizing: border-box;

					.right {
						flex: 1;

						.infoli {
							font-size: 12px;
							color: #000;
							margin-top: 5px;
							display: flex;
							justify-content: space-between;

							.infotitle {
								font-size: 14px;
								font-weight: 550;
							}


							.price {
								font-weight: 500;
								font-size: 12px;
								margin-top: 5px;
								color: #333;
							}


							text {
								margin-right: 5px;
							}
						}
					}

				}

				.infobot {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					border-top: 1px solid #f3f3f3;
					padding: 5px 10px;
					color: #000;
					font-size: 14px;

					button {
						margin: 0;
						padding: 0;
						line-height: normal;
					}

					.type {
						background-color: #050B18;
						color: #fff;
						padding: 5px 10px;
						border-radius: 5px;
						margin-left: 8px;
						font-size: 12px;
					}

					.type_1 {
						background-color: #fff;
						border: 1px solid #050B18;
						color: #050B18;
					}
				}

				.money {
					display: flex;
					justify-content: space-between;
					color: #050B18;
					font-size: 12px;
					font-weight: bold;
				}

				.buytime {
					display: table;
					width: 100%;
					font-size: 12px;
					box-sizing: border-box;

					text {
						float: left;
						margin: 0px 4% 0px 0;
					}
				}
			}
		}
	}
</style>